//  Parameter details refer to:https://support.huaweicloud.com/api-eip/BatchCreatePublicips.html
resource "huaweicloud_evs_volume" "volume" {
  count             = var.volume_configuration != null ? length(var.volume_configuration) : 0
  name              = var.name_suffix != "" ? format("%s-%s", lookup(element(var.volume_configuration, count.index), "name"), var.name_suffix) : lookup(element(var.volume_configuration, count.index), "name")
  volume_type       = lookup(element(var.volume_configuration, count.index), "volume_type")
  size              = lookup(element(var.volume_configuration, count.index), "volume_size")
  availability_zone = var.availability_zone
  charging_mode     = var.charging_mode
  period_unit       = var.period_unit
  period            = var.period
  tags = merge(
    { "Name" = var.name_suffix != "" ? format("%s-%s", lookup(element(var.volume_configuration, count.index), "name"), var.name_suffix) : lookup(element(var.volume_configuration, count.index), "name")},
    lookup(element(var.volume_configuration, count.index), "tags")
  )
}

//  Bind a data disk to an ECS instance
resource "huaweicloud_compute_volume_attach" "attached" {
  count       = var.attached_configuration != null ? length(var.attached_configuration) : 0
  instance_id = lookup(element(var.attached_configuration, count.index), "instance_id")
  volume_id   = huaweicloud_evs_volume.volume[count.index].id
}

